<template>
	<view class="content">
		<view class="head">
			<image src="/static/login26_bg.png"></image>
		</view>
		<view class="title">
			<text class="name">Hi,{{greeting == null ? '您好':greeting}}!</text>
			<text class="sub-name">欢迎登录{{$sysName}}~</text>
		</view>
		<view class="form-box">
			<view class="row-input">
				<image src="/static/login26_account.png"></image>
				<input v-model="form.username" placeholder="请输入账号" />
			</view>
			<view class="row-input">
				<image src="/static/login26_password.png"></image>
				<input v-model="form.password" placeholder="输入密码" password />
			</view>
			<view class="row-input">
				<image src="/static/login26_password.png"></image>
				<input type="text" v-model="form.code" placeholder="输入验证码" maxlength="4" />
				<image :src="randomImage" class="random-image" @click="getRandomImage"></image>
			</view>
			<view class="menu-link">
				<text @click="register">注册</text>
				<text @click="resetpwd">忘记密码?</text>
			</view>
			<view class="login-btn" @click="login">登录</view>
			<view class="tip-link">
				登录即代表你已同意<text>用户协议</text>和<text>隐私政策</text>
			</view>
		</view>
	</view>
</template>

<script>
	import api from "@/common/apiUtil.js"
	export default {
		data() {
			return {
				form:{
					username: null,
					password: null,
					code: null,
					codeKey:null
				},
				randomImage: null,
				greeting:null
			}
		},
		onShow() {
			let token = uni.getStorageSync("token");
			if(token){
				uni.reLaunch({
					url:"/pages/index/index"
				})
				return;
			}
			
			this.getRandomImage();
			this.setGreeting();
		},
		methods:{
			getKey(){
				let timestamp = Date.now();
				this.form.codeKey = timestamp;
				return timestamp;
			},
			setGreeting() {
			  const now = new Date();
			  const hours = now.getHours();
		
			  if (hours < 5) {
				this.greeting = '凌晨好';
			  } else if (hours < 12) {
				this.greeting = '上午好';
			  } else if (hours < 18) {
				this.greeting = '下午好';
			  } else {
				this.greeting = '晚上好';
			  }
			},
			getRandomImage(){
				let timestamp = this.getKey();
				console.log(api.GET_RANDOM_IMAGE+timestamp,"url")
				this.$get(api.GET_RANDOM_IMAGE+timestamp).then(res=>{
					console.log(res)
					this.randomImage = res.result;
				})
			},
			register(){
				uni.navigateTo({
					url: '/pages/register/register'
				});
			},
			resetpwd(){
				uni.showToast({
					title: '此平台禁止找回密码，请联系管理员重置!',
					icon:"none"
				});
			},
			login() {
			    console.log(this.form);
			
			    // 检查用户名是否为空
			    if (!this.form.username) {
			        uni.showToast({ title: '请输入您的账号', icon: 'none' });
			        return;
			    }
			
			    // 检查密码是否为空
			    if (!this.form.password) {
			        uni.showToast({ title: '请输入您的密码', icon: 'none' });
			        return;
			    }
			
			    // 检查验证码是否为空
			    if (!this.form.code) {
			        uni.showToast({ title: '请输入验证码', icon: 'none' });
			        return;
			    }
			
			    // 发送登录请求
			    this.$post(api.USER_LOGIN, this.form).then(res => {
			        console.log(res, "用户登录返回值");
			        uni.hideLoading();
			        // 检查返回码是否为200，如果是则表示登录成功
			        if (res.code == 200 && res.result != null) {
						uni.showToast({
						    title: res.message,
						    icon: "none"
						});
			            uni.setStorageSync("token", "Bearer " + res.result);
			
			            // 登录成功后跳转到首页
			            setTimeout(function() {
			                uni.reLaunch({
			                    url: "/pages/index/index"
			                });
			            }, 1000);
			        }else{
						uni.showToast({
						    title: res.message,
						    icon: "none"
						});
						this.form.code = null;
						setTimeout(()=>{
							this.getRandomImage();
						},2000)
					}
			    })
			}
		},
	}
</script>

<style lang="scss">
	page {
		background-color: #e9edf6;
	}

	.head {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		width: 100%;
		height: 30vh;
		z-index: -1;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.title {
		padding-left: 80rpx;
		padding-top: 100rpx;
		display: flex;
		justify-content: center;
		align-items: flex-start;
		flex-direction: column;
		height: 25vh;

		.name {
			font-size: 70rpx;
			line-height: 100rpx;
			font-weight: bold;
		}

		.sub-name {
			line-height: 80rpx;
			font-size: 45rpx;
			letter-spacing: 8rpx;
			font-weight: bold;
		}
	}

	.form-box {
		padding: 0rpx 80rpx;

		.row-input {
			display: flex;
			justify-content: flex-start;
			align-items: center;
			height: 100rpx;
			background-color: #FFFFFF;
			border-radius: 12rpx;
			margin: 30rpx 0rpx;

			image {
				margin: 0 25rpx;
				flex-shrink: 0;
				width: 31rpx;
				height: 35rpx;
			}

			input {
				font-size: 30rpx;
				flex: 1;
			}
			
			.random-image{
				width: 180rpx;
				height: 80rpx;
				margin-right: 10rpx;
			}
		}

		.menu-link {
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 26rpx;
		}

		.login-btn {
			margin-top: 50rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 90rpx;
			border-radius: 12rpx;
			background-color: #819682;
			color: #f4f4f4;
			font-size: 30rpx;
			letter-spacing: 5rpx;
			box-shadow: 0rpx 5rpx 20rpx #94ac95;
		}

		.tip-link {
			display: flex;
			justify-content: center;
			letter-spacing: 3rpx;
			line-height: 150rpx;
			font-size: 26rpx;

			text {
				color: #57a2ee;
			}
		}

	}
</style>
